home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7258 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.7 KB  |  129 lines

  1. Path: news1.h1.usa.pipeline.com!usenet
  2. From: grantp@usa.pipeline.com(Pete)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: C calling C++ function !!!
  5. Date: 22 Feb 1996 11:52:13 GMT
  6. Organization: Kalevi, Inc.
  7. Message-ID: <4ghldd$rt@news1.usa.pipeline.com>
  8. NNTP-Posting-Host: pipe7.h1.usa.pipeline.com
  9. X-PipeUser: grantp
  10. X-PipeHub: usa.pipeline.com
  11. X-PipeGCOS: (Pete)
  12. X-Newsreader: Pipeline USA v3.3.0
  13.  
  14. On Feb 21, 1996 12:24:45 in article <Re: C calling C++ function !!!>,
  15. 'a-kovalenko@star.nmb.ru (Alexey Kovalenko)' wrote: 
  16.  
  17.  
  18. >Hello, all. 
  19. >Few days ago Norm Bryar <normanb@halcyon.com> in his answer has written: 
  20. >>Something tells me it's not that simple.   
  21. >> 
  22. >[Skipped] 
  23. >>I believe the accepted thing to do is define a set of 'C' wrapper APIs 
  24. >>that you can export to your 'C' modules.  Internally, your wrappers do 
  25. >>class method calls.   
  26. >It's wrong. Function in C++, defined as "C", just uses predefined C
  27. calling  
  28. >convention instead compiler's preferred. 
  29.  
  30. What?  Come again, please. 
  31. >Real solution here (don't say "It will not work" - I'm using this ): 
  32. Well, then you haven't posted all of the relevant code.  Even 
  33. "filling the obvious blanks" will not produce workable code without 
  34. and extern "C" somewhere.  In particular, the symbol Msg is defined 
  35. in the c++ module, which is referenced in the c code.  C++ produces 
  36. a mangled version while the c code looks for _Msg.  It won't link. 
  37.  
  38. >Situation : I'm working on project that contain both C and C++ sources and
  39. I  
  40. >don't want to make C sources C++. I'm needing to call function-classmember
  41.  
  42. >from C sources . Function must return char* by index(access to indexed
  43. strings  
  44. >array, fuction in C used instead operator[] in C++).  
  45. >header contains: 
  46. >typedef char * (*MsgFunc)(int iNo ); 
  47. >C++ file contains: 
  48. >#include "header.h" 
  49. >#include "cassdef.hpp" 
  50. >static Message * _Msg = new Message("messages.msg"); 
  51. >static Message & Msg1 = *_Msg; 
  52. >// Two variables to avoid compiler warning about 
  53. >char * Msg( int iNo ) 
  54. >{ 
  55. >return Msg1[iNo];        // operator [] 
  56. >} 
  57. >MsgFunc Msg = Msg1; 
  58. >And C file contains: 
  59. >#include "header.h" 
  60. >extern MsgFun Msg; 
  61. >{ 
  62. >printf( Msg(3) ); 
  63. >} 
  64. >So, I'm calling C++ opertaor and class initialises before the "main"
  65. function. 
  66.  
  67. Even if this did work, it would still be an unacceptable solution to 
  68. most situations.  First, the real question was about whether or 
  69. not one can call class member functions from C, the implication 
  70. being that the function being a non-static member requiring the 
  71. caller to furnish the instance to which the member function is 
  72. to be applied.  There's no way to do this (portably or using standard 
  73. language features). Alexy's example does not provide for 
  74. specifying the object. 
  75.  
  76. Second, this kind of "hacking" is not acceptable to most project 
  77. supervisors as it creates unmaintanable code.  If the intent is 
  78. to call a c++ function from C, the most straightforward way is 
  79. to use an API intermediary.  The cost of an extra function call 
  80. is negligible compared with having to pay someone to try to 
  81. figure out what's going on when something goes wrong or there's 
  82. a change to system requirements. 
  83.  
  84. Compare Alexy's "messy" solution with a simple: 
  85.  
  86. extern "C" char * Msg(int); 
  87.  
  88. char * Msg(int i) 
  89.  { 
  90.    return SomeObject.Msg[i]; 
  91.  } 
  92.  
  93. Note that the above simple solution still fails the ability to  
  94. specify the object.  Code permitting that would be something 
  95. like: 
  96.  
  97. extern "C" char * Msg(void * obj, int i); 
  98.  
  99. char * Msg(void * obj, int i) 
  100.  {    
  101.     return ((SomeObject*)obj)->Msg(i); 
  102.  } 
  103.  
  104. Of course, it's now up to the programmer to ensure that the object 
  105. is of the correct class -- but that's unavoidable if passing 
  106. object pointers to C and back. 
  107.  
  108.  
  109. -- 
  110. Pete Grant 
  111. Kalevi, Inc. 
  112. Software Engineering & development
  113.